package com.tai.cobrapp.activity;

import com.tai.cobrapp.R;
import com.tai.cobrapp.activity.crud.NewSurveyActivity;
import com.tai.cobrapp.adapter.EstablishmentSelectionListAdapter;
import com.tai.cobrapp.backend.Backend;
import com.tai.cobrapp.model.Establecimiento;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class EstablishmentSelectionActivity extends ListActivity{
	EstablishmentSelectionListAdapter adapter = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_surveys);

		adapter = new EstablishmentSelectionListAdapter(this, Backend.getInstance().getActiveEstablecimientos(null, this.getListView().getContext()));
		this.getListView().setAdapter(adapter);
		
		this.getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				// aca obtener el id del elemento y luego ir a editarlo por ejemplo, o mostrar detalles
				// pasandole el ID como extras a otra actividad
				//Log.d("Click", ((Establecimiento)adapter.getItem(position)).getName().toString());
				//Log.d("ID Establ", ((Establecimiento)adapter.getItem(position)).getId().toString());
				// TODO : verificar que tenga detalle asociado??
				//Toast.makeText(EstablishmentSelectionActivity.this, "ID Establ "+((Establecimiento)adapter.getItem(position)).getId(), Toast.LENGTH_SHORT).show();
				if(chkDevicesExists(((Establecimiento)adapter.getItem(position)).getId())){
					Intent i = new Intent(EstablishmentSelectionActivity.this, NewSurveyActivity.class);
					i.putExtra("idEstablecimiento", ((Establecimiento)adapter.getItem(position)).getId());
					startActivity(i);					
				}else{
					Toast.makeText(EstablishmentSelectionActivity.this, R.string.there_are_no_devices, Toast.LENGTH_SHORT).show();
				}
				
				
				// Le paso el objeto completo DEL TIPO SERIALIZABLE
			}
		
		});
	}
	@Override
	public void onBackPressed() {
		Intent i = new Intent(EstablishmentSelectionActivity.this, OptionsMenuActivity.class);
		startActivity(i);
		super.onBackPressed();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	@Override
	protected void onStop() {
		finish();
		super.onStop();
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case R.id.main_menu:
			Intent i = new Intent(this, OptionsMenuActivity.class);
			startActivity(i);
			break;
		case R.id.log_out:
			Intent i2 = new Intent(this, LoginActivity.class);
			startActivity(i2);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	private boolean chkDevicesExists(Integer idEstablecimiento){
		return Backend.getInstance().bkEndchkDevicesExists(this, idEstablecimiento);
	}
}
